package meituan;

import java.util.Scanner;

/**
 * @author Hang
 * @date 2022/8/13
 * @project AutumnOffer
 * @description
 **/
public class BinaryTree {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int[] f = new int[n];
        for(int i=0;i<n;i++){
            f[i] = scanner.nextInt();
        }
        int dfs = dfs(f, 1, 0);
        System.out.println(dfs);
    }
    public static int dfs(int[] tree,int i,int sum){
        if(i<=tree.length+1&&2*i<=tree.length+1&&2*i<=tree.length){
            sum+=Math.max(dfs(tree,2*i,tree[i-1]),dfs(tree,2*i+1,tree[i-1]));
        }else {
            sum = sum;
        }
        return sum;
    }
}
